package com.controller;

import com.common.api.ApiResult;
import com.dto.FlyCircleParam;
import com.dto.FlyLineParam;
import com.dto.FlyRectangleParam;
import com.entity.MyHomePosition;
import com.service.DroneService;
import io.dronefleet.mavlink.common.HomePosition;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;

/**
 * @className: DroneController
 * @description:
 * @author: Kuangkeli
 * @date: 2024/07/01 16:00
 **/
@RestController
@RequestMapping("/drone")
public class DroneController {
    @Autowired
    private DroneService droneService;

    @PostMapping ("/setAutoTakeoffMode")
    public ApiResult setAutoTakeOff() throws IOException {
       droneService.setAutoTakeoffMode();
       return ApiResult.success("设置自动起飞模式指令已发送");
    }

    @PostMapping ("/sendLandCommand")
    public ApiResult sendLandCommand() throws IOException {
        droneService.sendLandCommand();
        return ApiResult.success("降落指令已发送");
    }

//    @PostMapping("/flyRectangle")
//    public ApiResult sendFlyRectangleCmd(@RequestBody FlyRectangleParam flyRectangleParam) throws IOException, InterruptedException {
//        droneService.sendflyRectangleCmdOnOffBoard(flyRectangleParam.getLength(),
//                flyRectangleParam.getWidth(),flyRectangleParam.getCurrent_x(),
//                flyRectangleParam.getCurrent_y(),flyRectangleParam.getFlightAltitude(),
//                flyRectangleParam.isClockwise(),flyRectangleParam.getCycle_s());
//        return ApiResult.success("执行矩形轨迹飞行指令已发送");
//    }

    @PostMapping("/flyRectangleByP2P")
    public ApiResult sendFlyRectangleByP2PCmd(@RequestBody FlyRectangleParam flyRectangleParam) throws IOException, InterruptedException {
        droneService.flyRectangleByP2P(flyRectangleParam.getFlightAltitude(),
                flyRectangleParam.getEndX(), flyRectangleParam.getEndY(),
                flyRectangleParam.getCurrent_x(), flyRectangleParam.getCurrent_y(),
                flyRectangleParam.isClockwise(),flyRectangleParam.getCycle_s());
        return ApiResult.success("执行矩形轨迹飞行指令(P2P)已完成");
    }

    @PostMapping("/flyCircle")
    public ApiResult sendFlyCirlceCmd(@RequestBody FlyCircleParam flyCircleParam) throws IOException, InterruptedException {
        droneService.sendflyCircleCmdOnOffBorad(flyCircleParam.getRadius(),
                flyCircleParam.isClockwise(), flyCircleParam.getCurrent_x(),
                flyCircleParam.getCurrent_y(), flyCircleParam.getFlightAltitude(), flyCircleParam.getCycle_s(),flyCircleParam.isIsup());
        return ApiResult.success("执行飞行圆形轨迹指令已完成");
    }

    @PostMapping("/flyLine")
    public ApiResult sendFlyLineCmd(@RequestBody FlyLineParam flyLineParam) throws IOException, InterruptedException {
        droneService.flyPointToPoint(flyLineParam.getStartX(),flyLineParam.getStartY(),
                flyLineParam.getEndX(), flyLineParam.getEndY(), flyLineParam.getFlightAltitude(),
                flyLineParam.getDuration_s());
        return ApiResult.success("执行飞行直线轨迹指令已完成");
    }

    @PostMapping ("/return")
    public ApiResult setAutoReturnMode() throws IOException {
        droneService.setAutoReturnMode();
        return ApiResult.success("自动返航指令已完成");
    }

    @PostMapping("/setAutoLandMode")
    public ApiResult setAutoLandMode() throws IOException {
        droneService.setAutoLandMode();
        return ApiResult.success("设置自动降落模式指令已完成");
    }

    @PostMapping("/getHomePosition")
    public ApiResult getHomePosition() throws IOException, InterruptedException {
        HomePosition homePosition = droneService.requestHomePosition();
        MyHomePosition myHomePosition = new MyHomePosition(homePosition.latitude(),homePosition.longitude(),homePosition.altitude(),homePosition.x(),homePosition.y(),homePosition.z());
        return ApiResult.success(myHomePosition);
    }

    @GetMapping ("/test")
    public ApiResult test() throws IOException, InterruptedException {
        HomePosition homePosition = droneService.requestHomePosition();
        MyHomePosition myHomePosition = new MyHomePosition(homePosition.latitude(),homePosition.longitude(),homePosition.altitude(),homePosition.x(),homePosition.y(),homePosition.z());
        return ApiResult.success(myHomePosition);
    }

    @GetMapping ("/getFlightRealDataListByTimeId")
    public ApiResult getFlightRealDataListByTimeId (@RequestParam String timeId) {
        return ApiResult.success(droneService.getFlightRealDataListByTimeId(timeId));
    }

    @GetMapping ("/getAllRecordsTimeId")
    public ApiResult getAllRecordsTimeId () {
        return ApiResult.success(droneService.getAllTimeId());
    }

    @PostMapping("/disarm")
    public ApiResult disArm() throws IOException {
        droneService.disArm();
        return ApiResult.success("上锁指令已发送");
    }
}